﻿using System;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace Blueprint.Mobile
{
    class MainController : IController
    {
        private IMainView view;
        private ServiceClient service;

        public MainController(IMainView view)
        {
            View = view;

            ServiceClient.EndpointAddress = new EndpointAddress("http://192.168.0.100:5610/Service.svc/basic");
            service = new ServiceClient();

            Category[] categories = service.GetCategories();
            view.SetCategories(categories);
        }

        private void attachView(IMainView view)
        {
            this.view = view;
            view.CategorySelected += new EventHandler(view_CategorySelected);
            view.Done += new EventHandler(view_Done);
        }

        void view_CategorySelected(object sender, EventArgs e)
        {
            //Category category = service.GetCategory(view.SelectedCategory.CategoryID);
            Category category = view.SelectedCategory;
            string s = string.Empty;
            foreach(Product product in category.Products)
                s += product.ProductName + "\r\n";
            view.SetProducts(s);
        }

        void view_Done(object sender, EventArgs e)
        {
            //view.Close();
            Application.Exit();
        }

        #region IController Members
        public IView View
        {
            get { return view; }
            set { attachView(value as IMainView); }
        }
        #endregion
    }
}
